package cn.appsys.controller.dev;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ImgController {

	//showImg?fileName=1573175067420_logo.jpg
	@RequestMapping("showImg")
	public void showImg(String fileName, HttpServletResponse response) {
		try {
			File file = new File("E:\\uploadfiles\\" + fileName);
			byte[] bytes = new byte[(int) file.length()];
			FileInputStream fis = new FileInputStream(file);
			fis.read(bytes);
			
			response.getOutputStream().write(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("download")
	public void download(String fileName, HttpServletResponse response) {
		try {
			File file = new File("E:\\uploadfiles\\" + fileName);
			byte[] bytes = new byte[(int) file.length()];
			FileInputStream fis = new FileInputStream(file);
			fis.read(bytes);
			
			//设置相应信息
			response.setContentType("application/octet-stream;charset=utf-8");
			response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);//原名
			response.setHeader("Content-Length", String.valueOf(file.length()));
			
			response.getOutputStream().write(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
